package com.we823.project.repository.service.repo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.common.collect.Maps;
import com.we823.project.model.repo.CategoryModel;
import com.we823.project.repository.dao.repo.CategoryRepository;
import com.we823.project.repository.service.AbstractBaseService;

@Service("categoryService")
public class CategoryService extends AbstractBaseService<CategoryModel> {

	private CategoryRepository categoryRepository;
	
	@Autowired
	public CategoryService(CategoryRepository categoryRepository) {
		super(categoryRepository);
		this.categoryRepository = categoryRepository;
	}

	public CategoryModel findByCode(String categoryCode) {
		return categoryRepository.findByCode(categoryCode);
	}

	public List<CategoryModel> findByCodeIn(List<String> codeList) {
		return categoryRepository.findByCodeIn(codeList);
	}

	public Map<Integer, CategoryModel> map() {
		List<CategoryModel> categoryModels = findAll();
		if(categoryModels!=null) {
			Map<Integer, CategoryModel> map = new HashMap<Integer, CategoryModel>();
			for (CategoryModel categoryModel : categoryModels) {
				map.put(categoryModel.getId(), categoryModel);
			}
			return map;
		}
		return null;
	}

	public Map<String, CategoryModel> mapByName() {
		List<CategoryModel> categoryModels = findAll();
		if (categoryModels!=null) {
			Map<String, CategoryModel> map = Maps.newHashMap();
			for (CategoryModel categoryModel : categoryModels) {
				map.put(categoryModel.getName(), categoryModel);
			}
			return map;
		}
		return null;
	}

}
